package Client.GUI;
/**
 * 
 * /**
 * 
 * @author Baglama Team
 *
 *
 */
 

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import Server.Message.MessageAddSchoolClass;
import Server.Message.MessageAddSchoolClassReplay;
import Client.logic.ClientIF;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.awt.Color;
import java.io.File;
import java.io.IOException;

/**
 *  secratery initializes add class panel
 */
public class AddClassPanel extends MyJPanel {
	/**
	 //private static final long serialVersionUID = 1L;
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textField;
	private JLabel lblNewLabel;
	private JButton btnCreate;
	/**
	 ** This is the  constructor
	 */
	public AddClassPanel(ClientIF client) {
		super(PanelType.ADD_CLASS_PANEL,client);
		setLayout(null);
		
		BufferedImage myPic;
		try {
			myPic = ImageIO.read(new File("images"+ File.separator+"add class.jpg"));
		
			JLabel lblAddClass = new JLabel(new ImageIcon(myPic), JLabel.CENTER);
			lblAddClass.setBounds(30, 13, 408, 115);
			add(lblAddClass);
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		JLabel lblEnterClassName = new JLabel("class name:");
		lblEnterClassName.setBounds(110, 141, 92, 17);
		add(lblEnterClassName);
		
		textField = new JTextField();
		textField.setBounds(229, 139, 112, 20);
		add(textField);
		textField.setColumns(10);
		/**
		 * This method initializes create button	
		 */
		btnCreate = new JButton("create");
		btnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (textField.getText().isEmpty())
					lblNewLabel.setText("Please type class name...");
				else{
					MessageAddSchoolClass newClass = new MessageAddSchoolClass(textField.getText());
					getClient().sendMsgToServer(newClass);
					MessageAddSchoolClassReplay classReplay = (MessageAddSchoolClassReplay) getClient().getMessageFromServer();
					if (classReplay.isPass()) getClient().swap(new AssignStudentToClassPanel(getClient()));
					else  lblNewLabel.setText("Class name exist please choose different name");
				}
			}
		});
		btnCreate.setBounds(195, 218, 89, 23);
		add(btnCreate);
		
		lblNewLabel = new JLabel("");
		lblNewLabel.setForeground(Color.RED);
		lblNewLabel.setBounds(42, 180, 372, 14);
		add(lblNewLabel);
	}
/**
 * @return new class 
 * */
	public MyJPanel pushPanel() {
		return new AddClassPanel(getClient());
	}
}
